<style lang="scss" scoped>
$width: 150px;
$height: 150px;
$color: #5F626A;

.hide {
  opacity: 0;
}

.upload-wrap {
  position: relative;
  display: inline-block;
  width: $width;
  min-height: $height;
  border: 3px dashed $color;
  // border-radius: 10px;

  #up-images {
    position: absolute;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    opacity: 0;
  }
  .pre-img {
    // width: $width;
    // min-height: $height;
    padding: 3px;
    box-sizing: border-box;
    border-radius: 10px;
    position: absolute;
    top: 0;
    left: 0;
  }

  label {
    position: relative;
    display: block;
    cursor: pointer;

    span {
      display: block;
      text-align: center;

      &:before {
        display: inline-block;
        padding-bottom: 100%;
        content: '';
        vertical-align: middle;
      }
    }
  }
}
.operate-wrap {
  margin-top: 20px;
}
.btn {
  display: inline-block;
  padding: 3px 25px;
  border: none;
  // border: 1px solid $color;
  border-radius: 3px;
  background: #68c39f;
  color: white;
  text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.1);
  font-size: 16px;
  font-weight: bold;

  &.disable {
    background: #c8ccd1;
    border: 1px solid #c8ccd1;
    cursor: not-allowed;
    &:hover {
      background: #c8ccd1;
    }
  }

  &:focus {
    outline: none;
  }

  &:hover {
    background: #20b9a4;
  }
}
.result-wrap {
  padding: 0;
  margin-top: 50px;
  font-size: 18px;
}
li {
  list-style: none;
  display: flex;
  span {
    flex: 1;
    text-align: right;
    padding: 0 5px;

    &:last-child {
      text-align: left;
    }

  }
}
</style>
<template>
  <Layout>
    <div class="upload-wrap">
      <img
        class="pre-img"
        alt="预览图片"
        width="150px"
        height="150px"
        :class="{ 'hide': !imgSrc }"
        :src="imgSrc">
      <div
        class="input-wrap"
        :class="{ 'hide': imgSrc }">
        <input
          name="images"
          id="up-images"
          type="file"
          accept=".jpg, .jpeg, .png"
          @change="fileChange"
          multiple="multiple" />
        <label for="up-images">
          <span>选择图片</span>
        </label>
      </div>
    </div>
    <div class="operate-wrap">
      <button
        type="button"
        @click="upload"
        :class="{ disable: !file }"
        :disabled="!file"
        class="btn">上传</button>
    </div>
    <ul class="result-wrap">
      <li v-for="item in results">
        <span>结果: {{item[0]}}</span>,<span> 概率: {{item[1] * 100}}%</span>
      </li>
    </ul>
  </Layout>
</template>
<script>
  import ApiService from '../../common/apiService';
  import Utils from '../../utils/index';

  export default {
    data() {
      return {
        isLoading: true,
        file: null,
        imgSrc: null,
        results: [],
      };
    },
    methods: {
      fileChange(e) {
        if (e.target.files && e.target.files[0]) {
          this.file = e.target.files[0];
          this.readURL(e.target);
        } else {
          this.file = null;
        }
      },
      readURL(input) {
        const reader = new FileReader();
        reader.onload = e => {
          this.imgSrc = e.target.result;
        };
        reader.readAsDataURL(input.files[0]);
      },
      upload() {
        const dataForm = new FormData();
        dataForm.append('img', this.file);
        ApiService.uploadImg(dataForm)
          .then(res => {
            this.results = res.results;
          }, error => {
            console.error('error', error);
            Utils.createNoty('上传图片错误，请重试！', 'error');
          });
      },
    },
    created() {
    },
    components: {
    },
  };
</script>
